package com.acme.bankapp.sc.bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.acme.bankapp.domain.bank.Bank;

public class BankClient {
	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	String[] mess;
	Bank b;

	BankClient() {
	}

	void run() {
		try {
			requestSocket = new Socket("localhost", 2004);
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			in = new ObjectInputStream(requestSocket.getInputStream());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			System.out
					.println("Please enter the array of strings in one of three available format: ");
			System.out.println("If you want to create new client:");
			System.out
					.println("1 accounttype=c|s;balance=100;overdraft=50;name=John;gender=m|f;");
			System.out.println("If you want to deposite some account:");
			System.out.println("2 idOfClient numOfAccount sum");
			System.out.println("If you want to withdraw some account:");
			System.out.println("3 idOfClient numOfAccount sum");
			System.out.println("Print bye to close connection");
			do {
				mess = br.readLine().trim().split(" ");
				out.writeObject(mess);
				try {
					 b = (Bank) in.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			} while (!mess[0].equals("bye"));

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
				requestSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}

	}

	public static void main(String args[]) {
		BankClient client = new BankClient();
		client.run();
	}
}
